#'
#' The effective number of species, Hill number
#' 
#' @param com a community object
#' @param q a non-negative integer that defines the particular Hill number
#'
#' @details
#' Hill numbers (Hill 1973) are a family of diversity indices that overcome the problems of many of the diversity indices
#' most commonly used by ecologists. Hill numbers preserve the doubling property, they quantify diversity in units of 
#' modified species counts, and they are equivalent to algebraic transformations of most other indices.
#' 
#' The general formula for the calculation of a Hill number is:
#' 
#' q^D=(sum_\{i\}(p_i^q))^(1/(1-q))
#' 
#' p_i is the "true" relative frequency of each of species in the complete assemblage. Changing q yields a family of diversity
#' indices. 
#' 
#' As q increases, the index puts increasing weight on the most common species in the assemblage, with rare species
#' making less and less of contribution to the summation. Once q is larger than 5, Hill number rapidly converge to 
#' the inverse of the relative abundance of the most common species. Negative values for q are theoretically possible,
#' but they are never used as diversity indices because they place too much weight on the frequencies of rare species,
#' which are dominated by sampling noise. As q increases, the diversity index decreases, unless all species are equally
#' abundant. In this case, the Hill number is the same for all values of q, and is equivalent to simple species richness.
#' 
#' @references
#' Hill, M. O. 1973. Diversity and evenness: a unifying notation and its consequences. Ecology, 54:427-432.
#' 
#' Gotelli, N.J. and Ellison, M. 2013. A primer of ecological statistics (2nd Edition). Sinauer Associates.
#'
#'@examples
#'data(BCI)
#'
#'hill_number(BCI,q=0)
#'hill_number(BCI,q=1)
#'hill_number(BCI,q=2)
#'hill_number(BCI,q=3)

hill_number=function(com,q=1){
  if(q<0)
    stop("it is meaningless if q is smaller than 0.")
  spab=species_abundance(com)
  p=spab/sum(spab)
  if(q==1)
    return(exp(shannon_diversity_index(spab)))
  else
    return((sum(p^q))^(1/(1-q)))
}